# Author: Lovy
# File : basePage
# Time : 2023-03-17 16:52
"""
基类 所有页面的公共部分
    1.打开网页
    2.获取素有元素
    3.输入文本
    4.点击元素
    5.获取元素的文本
"""
import os
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from common.common_driver import CommDriver
import warnings
from configs.env import Env
from utils.handle_path import PollyPath
from utils.handle_yaml import get_yaml_data


class BasePage:
    def __init__(self):
        self.driver = CommDriver().get_driver()
        # 1. self.__class__.__name__ 获取类名
        self.locators = get_yaml_data(PollyPath.configs_path / 'allelements.yaml')[self.__class__.__name__]
        # 2. 优化定位器 设置属性
        for element_name, element_locator in self.locators.items():
            setattr(self, element_name, element_locator)

    def open_url(self, url):
        """
        :param url:
        :return:
        """
        self.driver.get(url)

    def get_element_deprecated(self, locator):
        # locator 定位器 exp:("id", "ls_username")
        warnings.warn("这个方法已经弃用！get_element替用", DeprecationWarning)
        return self.driver.find_element(*locator)

    def get_element(self, locator, timeout=Env.TIMEOUT,
                    polly_frequency=Env.POLL_FREQUENCY, desc=None, elements=False):
        """
        保护： 如果定位不到 就截图写日志
        :param desc:
        :param elements:
        :param locator:
        :param timeout:
        :param polly_frequency:
        :return:
        """
        try:
            if not elements:
                return WebDriverWait(self.driver, timeout, polly_frequency).\
                    until(EC.visibility_of_element_located(locator))
            else:
                return WebDriverWait(self.driver, timeout, polly_frequency). \
                    until(EC.visibility_of_all_elements_located(locator))

        except:
            # 浏览器截图 文件名中间不能包含空格
            curtime = time.strftime("%Y%m%d%H%M%S")
            filename = f'{PollyPath.screenshots_path}{os.sep}{desc}{curtime}.png'
            # print(filename)
            self.driver.save_screenshot(filename)
            from utils.handle_loguru import log
            log.error(f'{desc}元素未找到')

    def input_text_old(self, locator, text, desc=None):
        warnings.warn("这个方法已弃用，使用input_text替代！", DeprecationWarning)
        self.get_element(locator, desc=desc).send_keys(text)

    def input_text(self, locator, text, append=False, desc=None):
        if append:
            self.get_element(locator, desc=desc).send_keys(text)
        else:
            self.get_element(locator, desc=desc).clear()
            self.get_element(locator, desc=desc).send_keys(text)

    def click_element(self, locator, desc=None):
        self.get_element(locator, desc=desc).click()

    def get_element_text(self, locator, desc=None):
        return self.get_element(locator, desc=desc).text

    def get_elements_text(self, locator, desc=None):
        return [ele.text for ele in self.get_element(locator, desc=desc, elements=True)]


if __name__ == '__main__':
    test_flag = 1
    if test_flag == 1:
        from pprint import pprint
        from pageObjects.loginPage import LoginPage
        test_basepage = LoginPage()
        pprint(test_basepage.locators)

    if test_flag == 10:
        test_basePage = BasePage()
        test_basePage.open_url(Env.HOST)
        ele_username_locator = ('id', 'username')
        test_basePage.get_element(ele_username_locator, desc="测试")
